home *** CD-ROM | disk | FTP | other *** search
-
- #line 2 "Newstuff A 5/26/87:Sample.y"
- /* this is a test file for macyacc It should be renamed as something.y.
- * MacYacc expects files with the extension '.y' or '.Y'
- * it then produces a compilable file with the extension '.c'.
- * If requested, it produces a '.h' include file and a '.o'
- * grammar file (a list of the states, shifts, etc.)
- * The '.c' file should be compiled and linked in a environment
- * that simulates a standard terminal (such as linking with the
- * Consular stdlib).
- * The parser code lives in the data fork of mac yacc. Extract it w/ FEDIT
- * or whatever if you would like to look at it or modify it.
- * As an additional bonus, yacc contains a resource type: cstr which others
- * may find useful.
- *
- * for additional information see:
- * Unix Utilities Manual (Steve Johnson)
- * The Unix Programming Environment (Kernighan & Pike) (excellent book)
- * Intro to Compiler Construction w/ Unix (Schreiner & Friedman)
- *
- * this program is copyrighted by no-one. Have fun.
- */
- #include <stdio.h>
- #define DIGIT 257
- #define UMINUS 258
- #define yyclearin yychar = -1
- #define yyerrok yyerrflag = 0
- extern int yychar;
- extern short yyerrflag;
- #ifndef YYMAXDEPTH
- #define YYMAXDEPTH 150
- #endif
- #ifndef YYSTYPE
- #define YYSTYPE int
- #endif
- YYSTYPE yylval, yyval;
- #define YYERRCODE 256
-
- # line 66 "Newstuff A 5/26/87:Sample.y"
-
- yylex()
- {
- int c;
-
- while((c = putchar(getchar())) == ' ');
-
- if( isdigit(c)) {
- yylval = c - '0';
- return(DIGIT);
- }
- else if(tolower(c) == 'q') c = 0;
- return(c);
- }
- yyerror(str)
- char *str;
- {
- printf("%s\n",str);
- return(0);
- }
- main()
- {
- printf("Calculate Now(q to quit)...\n");
- yyparse();
- }
- short yyexca[] ={
- -1, 1,
- 0, -1,
- -2, 0,
- };
- # define YYNPROD 15
- # define YYLAST 219
- short yyact[]={
-
- 5, 5, 4, 18, 7, 6, 6, 10, 16, 17,
- 9, 2, 1, 0, 19, 20, 21, 22, 23, 15,
- 0, 0, 0, 24, 13, 11, 15, 12, 15, 14,
- 0, 13, 11, 13, 12, 0, 14, 0, 14, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 3, 8, 8 };
- short yypact[]={
-
- -1000, -40, -3, -6, -11, -39, -39,-254,-1000,-1000,
- -1000, -39, -39, -39, -39, -39, -18,-1000,-1000, -9,
- -9,-1000,-1000,-1000,-1000 };
- short yypgo[]={
-
- 0, 12, 11, 2, 4 };
- short yyr1[]={
-
- 0, 1, 1, 1, 2, 3, 3, 3, 3, 3,
- 3, 3, 3, 4, 4 };
- short yyr2[]={
-
- 0, 0, 3, 3, 1, 3, 3, 3, 3, 3,
- 3, 2, 1, 1, 2 };
- short yychk[]={
-
- -1000, -1, -2, 256, -3, 40, 45, -4, 257, 13,
- 13, 43, 45, 42, 47, 37, -3, -3, 257, -3,
- -3, -3, -3, -3, 41 };
- short yydef[]={
-
- 1, -2, 0, 0, 4, 0, 0, 12, 13, 2,
- 3, 0, 0, 0, 0, 0, 0, 11, 14, 6,
- 7, 8, 9, 10, 5 };
- #define YYFLAG -1000
- #define YYERROR goto yyerrlab
- #define YYACCEPT return(0)
- #define YYABORT return(1)
-
- /* parser for yacc output */
-
- int yydebug = 0; /* 1 for debugging */
- YYSTYPE yyv[YYMAXDEPTH]; /* where the values are stored */
- int yychar = -1; /* current input token number */
- int yynerrs = 0; /* number of errors */
- short yyerrflag = 0; /* error recovery flag */
-
- yyparse() {
-
- short yys[YYMAXDEPTH];
- short yyj, yym;
- register YYSTYPE *yypvt;
- register short yystate, *yyps, yyn;
- register YYSTYPE *yypv;
- register short *yyxi;
-
- yystate = 0;
- yychar = -1;
- yynerrs = 0;
- yyerrflag = 0;
- yyps= &yys[-1];
- yypv= &yyv[-1];
-
- yystack: /* put a state and value onto the stack */
-
- if( yydebug ) printf( "state %d, char 0%o\n", yystate, yychar );
- if( ++yyps> &yys[YYMAXDEPTH] ) { yyerror( "yacc stack overflow" ); return(1); }
- *yyps = yystate;
- ++yypv;
- *yypv = yyval;
-
- yynewstate:
-
- yyn = yypact[yystate];
-
- if( yyn<= YYFLAG ) goto yydefault; /* simple state */
-
- if( yychar<0 ) if( (yychar=yylex())<0 ) yychar=0;
- if( (yyn += yychar)<0 || yyn >= YYLAST ) goto yydefault;
-
- if( yychk[ yyn=yyact[ yyn ] ] == yychar ){ /* valid shift */
- yychar = -1;
- yyval = yylval;
- yystate = yyn;
- if( yyerrflag > 0 ) --yyerrflag;
- goto yystack;
- }
-
- yydefault:
- /* default state action */
-
- if( (yyn=yydef[yystate]) == -2 ) {
- if( yychar<0 ) if( (yychar=yylex())<0 ) yychar = 0;
- /* look through exception table */
-
- for( yyxi=yyexca; (*yyxi!= (-1)) || (yyxi[1]!=yystate) ; yyxi += 2 ) ; /* VOID */
-
- while( *(yyxi+=2) >= 0 ){
- if( *yyxi == yychar ) break;
- }
- if( (yyn = yyxi[1]) < 0 ) return(0); /* accept */
- }
-
- if( yyn == 0 ){ /* error */
- /* error ... attempt to resume parsing */
-
- switch( yyerrflag ){
-
- case 0: /* brand new error */
-
- yyerror( "syntax error" );
- yyerrlab:
- ++yynerrs;
-
- case 1:
- case 2: /* incompletely recovered error ... try again */
-
- yyerrflag = 3;
-
- /* find a state where "error" is a legal shift action */
-
- while ( yyps >= yys ) {
- yyn = yypact[*yyps] + YYERRCODE;
- if( yyn>= 0 && yyn < YYLAST && yychk[yyact[yyn]] == YYERRCODE ){
- yystate = yyact[yyn]; /* simulate a shift of "error" */
- goto yystack;
- }
- yyn = yypact[*yyps];
-
- /* the current yyps has no shift onn "error", pop stack */
-
- if( yydebug ) printf( "error recovery pops state %d, uncovers %d\n", *yyps, yyps[-1] );
- --yyps;
- --yypv;
- }
-
- /* there is no state on the stack with an error shift ... abort */
-
- yyabort:
- return(1);
-
-
- case 3: /* no shift yet; clobber input char */
-
- if( yydebug ) printf( "error recovery discards char %d\n", yychar );
-
- if( yychar == 0 ) goto yyabort; /* don't discard EOF, quit */
- yychar = -1;
- goto yynewstate; /* try again in the same state */
-
- }
-
- }
-
- /* reduction by production yyn */
-
- if( yydebug ) printf("reduce %d\n",yyn);
- yyps -= yyr2[yyn];
- yypvt = yypv;
- yypv -= yyr2[yyn];
- yyval = yypv[1];
- yym=yyn;
- /* consult goto table to find next state */
- yyn = yyr1[yyn];
- yyj = yypgo[yyn] + *yyps + 1;
- if( yyj>=YYLAST || yychk[ yystate = yyact[yyj] ] != -yyn ) yystate = yyact[yypgo[yyn]];
- switch(yym){
-
- case 3:
- #line 37 "Newstuff A 5/26/87:Sample.y"
- { yyerrok;} break;
- case 4:
- #line 40 "Newstuff A 5/26/87:Sample.y"
- { printf("%d\n",yypvt[-0]); } break;
- case 5:
- #line 44 "Newstuff A 5/26/87:Sample.y"
- { yyval = yypvt[-1]; } break;
- case 6:
- #line 46 "Newstuff A 5/26/87:Sample.y"
- { yyval = yypvt[-2] + yypvt[-0]; } break;
- case 7:
- #line 48 "Newstuff A 5/26/87:Sample.y"
- { yyval = yypvt[-2] - yypvt[-0]; } break;
- case 8:
- #line 50 "Newstuff A 5/26/87:Sample.y"
- { yyval = yypvt[-2] * yypvt[-0]; } break;
- case 9:
- #line 52 "Newstuff A 5/26/87:Sample.y"
- { yyval = yypvt[-2] / yypvt[-0]; } break;
- case 10:
- #line 54 "Newstuff A 5/26/87:Sample.y"
- { yyval = yypvt[-2] % yypvt[-0]; } break;
- case 11:
- #line 56 "Newstuff A 5/26/87:Sample.y"
- { yyval = - yypvt[-0]; } break;
- case 13:
- #line 61 "Newstuff A 5/26/87:Sample.y"
- { yyval = yypvt[-0];} break;
- case 14:
- #line 63 "Newstuff A 5/26/87:Sample.y"
- { yyval = 10 *yypvt[-1] + yypvt[-0]; } break;
- }
- goto yystack; /* stack new state and value */
-
- }
-